<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      fixed
      title="面经详细"
       @click-left="$router.back()"

    />
    <header class="header">
      <h1>{{info.stem}}</h1>
      <p>
        {{info.createdAt}}|{{info.views}} 浏览量 |
        {{info.likeCount}} 点赞数
      </p>
      <p>
        <img :src="info.avatar" alt="" />
        <span>{{info.creator}}</span>
      </p>
    </header>
    <main v-html="info.content" class="body"></main>
    <div   class="opt">
      <van-icon @click="likeChange(1)" :class="{active: info.likeFlag == 1}"   name="like-o"/>
      <van-icon  @click="likeChange(2)" :class="{active: info.collectFlag == 1}" name="star-o"/>
    </div>
  </div>
</template>

<script>
import { detailAPI, zanAPI } from '@/api/interview'
export default {
  name: 'detail-page',
  data () {
    return {
      info: {}
    }
  },
  async created () {
    const res = await detailAPI({
      id: this.$route.params.id
    })
    // console.log(res)
    this.info = res.data
  },
  methods: {
    // 点赞的图标
    async  likeChange (flag) {
      if (flag === 1) {
        // 要么取消点赞，要么就是点赞
        this.info.likeFlag = !this.info.likeFlag
      } else {
        this.info.collectFlag = !this.info.collectFlag
      }

      await zanAPI({
        id: this.$route.params.id,
        optType: flag
      })
    }
  }

}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
